package qldc.pm.qlktcsdldc.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import qldc.pm.qlktcsdldc.model.DanhMucSoBanNganh;
import qldc.pm.qlktcsdldc.model.NguoiDung;
import qldc.pm.qlktcsdldc.portlet.QLKTCSDLDCContants;
import qldc.pm.qlktcsdldc.report.TaiKhoanReport;
import qldc.pm.qlktcsdldc.service.DanhMucSoBanNganhLocalServiceUtil;
import qldc.pm.qlktcsdldc.service.NguoiDungLocalServiceUtil;
import qldc.pm.qlktcsdldc.utils.FLog;
import qldc.pm.qtht.dmdc.constants.MaPhanMem;
import qldc.pm.service.lichsuthaotac.LichSuThaoTacEnum;
import qldc.pm.service.lichsuthaotac.ThaoTacConstant;
import qldc.pm.service.lichsuthaotac.util.LichSuThaoTacUtil;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.JavaConstants;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Address;
import com.liferay.portal.model.EmailAddress;
import com.liferay.portal.model.Phone;
import com.liferay.portal.model.User;
import com.liferay.portal.model.UserGroupRole;
import com.liferay.portal.model.Website;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.UserServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
import com.report.logic.Report;



public class QuanLyTaiKhoanAction {	
	private static final Log _Log = LogFactoryUtil.getLog(QuanLyTaiKhoanAction.class.getName());

	
	public void voHieuHoaNguoiDung(ActionRequest actionRequest, ActionResponse actionResponse) {
		FLog.Log("voHieuHoaNguoiDung");
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		try {
			User user = PortalUtil.getUser(actionRequest);
			if (Validator.isNotNull(user)) {
				long IDQLKT_NguoiDung = ParamUtil.getLong(actionRequest, "IDQLKT_NguoiDung");
				
				NguoiDung nguoiDung = NguoiDungLocalServiceUtil.kickHoatNguoiDung(IDQLKT_NguoiDung, false, user);
				if(Validator.isNotNull(nguoiDung)){
					FLog.Log("voHieuHoaNguoiDung  vo-hieu-hoa-nguoi-dung-thanh-cong");
					SessionMessages.add(actionRequest, "vo-hieu-hoa-nguoi-dung-thanh-cong");
				}else{
					FLog.Log("voHieuHoaNguoiDung  vo-hieu-hoa-nguoi-dung-khong-thanh-cong");
					SessionErrors.add(actionRequest, "vo-hieu-hoa-nguoi-dung-khong-thanh-cong");
				}
				
			}else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}

			if (redirect != null && redirect.length() > 0)
				actionResponse.sendRedirect(redirect);

		}
		catch (PortalException e) {
			_Log.error(e);
		}
		catch (SystemException e) {
			_Log.error(e);
		}
		catch (IOException e) {
			_Log.error(e);
		}
	}
	
	public void kichHoatNguoiDung(ActionRequest actionRequest, ActionResponse actionResponse) {
		FLog.Log("kichHoatNguoiDung");
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		try {
			User user = PortalUtil.getUser(actionRequest);
			if (Validator.isNotNull(user)) {
				long IDQLKT_NguoiDung = ParamUtil.getLong(actionRequest, "IDQLKT_NguoiDung");
				
				NguoiDung nguoiDung = NguoiDungLocalServiceUtil.kickHoatNguoiDung(IDQLKT_NguoiDung, true, user);
				if(Validator.isNotNull(nguoiDung)){
					FLog.Log("kichHoatNguoiDung  kich-hoat-nguoi-dung-thanh-cong");
					SessionMessages.add(actionRequest, "kich-hoat-nguoi-dung-thanh-cong");
				}else{
					FLog.Log("kichHoatNguoiDung kich-hoat-nguoi-dung-khong-thanh-cong");
					SessionErrors.add(actionRequest, "kich-hoat-nguoi-dung-khong-thanh-cong");
				}
				
			}else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}

			if (redirect != null && redirect.length() > 0)
				actionResponse.sendRedirect(redirect);

		}
		catch (PortalException e) {
			_Log.error(e);
		}
		catch (SystemException e) {
			_Log.error(e);
		}
		catch (IOException e) {
			_Log.error(e);
		}
	}
	
	public void deleteNguoiDung(ActionRequest actionRequest, ActionResponse actionResponse) {
		FLog.Log("deleteNguoiDung");
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		try {
			User user = PortalUtil.getUser(actionRequest);
			if (Validator.isNotNull(user)) {
				long IDQLKT_NguoiDung = ParamUtil.getLong(actionRequest, "IDQLKT_NguoiDung");
				
				NguoiDung nguoiDung = NguoiDungLocalServiceUtil.deleteNguoiDung(IDQLKT_NguoiDung, user);
				if(Validator.isNotNull(nguoiDung)){
					FLog.Log("deleteNguoiDung  xoa-nguoi-dung-thanh-cong");
					SessionMessages.add(actionRequest, "xoa-nguoi-dung-thanh-cong");
				}else{
					FLog.Log("deleteNguoiDung xoa-nguoi-dung-khong-thanh-cong");
					SessionErrors.add(actionRequest, "xoa-nguoi-dung-khong-thanh-cong");
				}
				
			}else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}

			if (redirect != null && redirect.length() > 0)
				actionResponse.sendRedirect(redirect);

		}
		catch (PortalException e) {
			_Log.error(e);
		}
		catch (SystemException e) {
			_Log.error(e);
		}
		catch (IOException e) {
			_Log.error(e);
		}
	}
	
	public void addNguoiDung(ActionRequest actionRequest, ActionResponse actionResponse) {
		FLog.Log("addNguoiDung");
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		try {
			User user = PortalUtil.getUser(actionRequest);
			if (Validator.isNotNull(user)) {
				long soBanNganhId = ParamUtil.getLong(actionRequest, "soBanNganh");
				String tenDangNhap = ParamUtil.getString(actionRequest, "tenDangNhap");
				String hoTen = ParamUtil.getString(actionRequest, "hoTen");
				String soCMND = ParamUtil.getString(actionRequest, "soCMND");
				int gioiTinh = ParamUtil.getInteger(actionRequest, "gioiTinh");
				long danTocId = ParamUtil.getLong(actionRequest, "danToc");
				
				long tinhId = ParamUtil.getLong(actionRequest, "tinh");
				long quanHuyenId = ParamUtil.getLong(actionRequest, "quanHuyen");
				long phuongXaId = ParamUtil.getLong(actionRequest, "phuongXa");
				String diaChi = ParamUtil.getString(actionRequest, "diaChi");
				String giaiThich = ParamUtil.getString(actionRequest, "giaiThich");
				
				String thaoTac = "";
				NguoiDung oldObject = null;
				NguoiDung newObject = null;
				
				try {
					User newUser = addUser(actionRequest, actionResponse, tenDangNhap, hoTen, gioiTinh);
					FLog.Log("addNguoiDung  Add user :"+ newUser.getUserId());
					newObject = NguoiDungLocalServiceUtil.addNguoiDung(newUser.getUserId(), tenDangNhap, hoTen, soCMND, tinhId, quanHuyenId, phuongXaId, diaChi, soBanNganhId, gioiTinh, danTocId, giaiThich, user);
					thaoTac = ThaoTacConstant.THEM_MOI;
					logChangedValues(user, oldObject, newObject, thaoTac);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					FLog.Log("addNguoiDung them-moi-nguoi-dung-khong-thanh-cong");
					SessionErrors.add(actionRequest, "them-moi-nguoi-dung-khong-thanh-cong");
				}
				
				
			}else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}

			if (redirect != null && redirect.length() > 0)
				actionResponse.sendRedirect(redirect);

		}
		catch (PortalException e) {
			_Log.error(e);
		}
		catch (SystemException e) {
			_Log.error(e);
		}
		catch (IOException e) {
			_Log.error(e);
		}
	}
	private User addUser(ActionRequest request, ActionResponse response, String tenDangNhap, String hoVaTen, int gioiTinh) throws Exception {
		ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

		boolean autoPassword = false;
		String password1 = "123456";
		String password2 = "123456";

		String reminderQueryQuestion = "";
		String reminderQueryAnswer = "";

		boolean autoScreenName = false;

		String screenName = tenDangNhap;
		String emailAddress = tenDangNhap + "@cabd.com";

		/*
		 * some default values for unused fields
		 */
		long facebookId = 0L;
		String openId = StringPool.BLANK;
		String languageId = StringPool.BLANK;
		String timeZoneId = StringPool.BLANK;
		String greeting = StringPool.BLANK;

		/*
		 * name fields
		 */
		String firstName = hoVaTen;
		String middleName = StringPool.BLANK;
		String lastName = StringPool.BLANK;

		int prefixId = 0;
		int suffixId = 0;

		boolean male = gioiTinh == 1 ? true : false;

		/*
		 * birthday information
		 */

		int birthdayMonth = 1;
		int birthdayDay = 1;
		int birthdayYear = 1980;

		/*
		 * social network fields
		 */
		String comments = StringPool.BLANK;
		String smsSn = StringPool.BLANK;
		String aimSn = StringPool.BLANK;
		String facebookSn = StringPool.BLANK;
		String icqSn = StringPool.BLANK;
		String jabberSn = StringPool.BLANK;
		String msnSn = StringPool.BLANK;
		String mySpaceSn = StringPool.BLANK;
		String skypeSn = StringPool.BLANK;
		String twitterSn = StringPool.BLANK;
		String ymSn = StringPool.BLANK;
		String jobTitle = StringPool.BLANK;

		long[] groupIds = null;
		long[] organizationIds = null;
		long[] roleIds = null;
		List<UserGroupRole> userGroupRoles = UsersAdminUtil
				.getUserGroupRoles(request);
		long[] userGroupIds = null;
		List<Address> addresses = new ArrayList<Address>();
		List<EmailAddress> emailAddresses = UsersAdminUtil
				.getEmailAddresses(request);
		List<Phone> phones = UsersAdminUtil.getPhones(request);
		List<Website> websites = UsersAdminUtil.getWebsites(request);
		List<AnnouncementsDelivery> announcementsDelivers = new ArrayList<AnnouncementsDelivery>();
		boolean sendEmail = false;

		Locale locale = LocaleUtil.getDefault();
		long companyId = themeDisplay.getCompanyId();

		ServiceContext serviceContext = ServiceContextFactory.getInstance(
				User.class.getName(), request);

		/*
		 * add user
		 */
		User user = UserServiceUtil.addUser(companyId, autoPassword,
				password1, password2, autoScreenName, screenName,
				emailAddress, facebookId, openId, locale, firstName,
				middleName, lastName, prefixId, suffixId, male,
				birthdayMonth, birthdayDay, birthdayYear, jobTitle,
				groupIds, organizationIds, roleIds, userGroupIds,
				addresses, emailAddresses, phones, websites,
				announcementsDelivers, sendEmail, serviceContext);

		if (!userGroupRoles.isEmpty()) {
			for (UserGroupRole userGroupRole : userGroupRoles) {
				userGroupRole.setUserId(user.getUserId());
			}

			user = UserServiceUtil.updateUser(user.getUserId(),
					StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
					false, reminderQueryQuestion, reminderQueryAnswer,
					user.getScreenName(), user.getEmailAddress(),
					facebookId, openId, languageId, timeZoneId, greeting,
					comments, firstName, middleName, lastName, prefixId,
					suffixId, male, birthdayMonth, birthdayDay,
					birthdayYear, smsSn, aimSn, facebookSn, icqSn,
					jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
					jobTitle, groupIds, organizationIds, roleIds,
					userGroupRoles, userGroupIds, addresses,
					emailAddresses, phones, websites,
					announcementsDelivers, serviceContext);
		}

		return user;
	}
	public void updateNguoiDung(ActionRequest actionRequest, ActionResponse actionResponse) {
		FLog.Log("updateNguoiDung");
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		try {
			User user = PortalUtil.getUser(actionRequest);
			if (Validator.isNotNull(user)) {
				long IDQLKT_NguoiDung = ParamUtil.getLong(actionRequest, "IDQLKT_NguoiDung");
				long soBanNganhId = ParamUtil.getLong(actionRequest, "soBanNganh");
				String tenDangNhap = ParamUtil.getString(actionRequest, "tenDangNhap");
				String hoTen = ParamUtil.getString(actionRequest, "hoTen");
				String soCMND = ParamUtil.getString(actionRequest, "soCMND");
				int gioiTinh = ParamUtil.getInteger(actionRequest, "gioiTinh");
				long danTocId = ParamUtil.getLong(actionRequest, "danToc");
				
				long tinhId = ParamUtil.getLong(actionRequest, "tinh");
				long quanHuyenId = ParamUtil.getLong(actionRequest, "quanHuyen");
				long phuongXaId = ParamUtil.getLong(actionRequest, "phuongXa");
				String diaChi = ParamUtil.getString(actionRequest, "diaChi");
				String giaiThich = ParamUtil.getString(actionRequest, "giaiThich");
				String thaoTac = ThaoTacConstant.CHINH_SUA;
				NguoiDung oldObject = NguoiDungLocalServiceUtil.fetchNguoiDung(IDQLKT_NguoiDung);
				NguoiDung newObject = null;
				
				updateUser(actionRequest, actionResponse,IDQLKT_NguoiDung, tenDangNhap, hoTen, gioiTinh);
				newObject = NguoiDungLocalServiceUtil.updateNguoiDung(IDQLKT_NguoiDung, tenDangNhap, hoTen, soCMND, tinhId, quanHuyenId, phuongXaId, diaChi, soBanNganhId, gioiTinh, danTocId, giaiThich, user);
				logChangedValues(user, oldObject, newObject, thaoTac);
				if(Validator.isNotNull(newObject)){
					FLog.Log("updateNguoiDung  sua-nguoi-dung-thanh-cong");
					SessionMessages.add(actionRequest, "sua-moi-nguoi-dung-thanh-cong");
				}else{
					FLog.Log("updateNguoiDung sua-nguoi-dung-khong-thanh-cong");
					SessionErrors.add(actionRequest, "sua-nguoi-dung-khong-thanh-cong");
				}
				
			}else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}

			if (redirect != null && redirect.length() > 0)
				actionResponse.sendRedirect(redirect);

		}
		catch (PortalException e) {
			_Log.error(e);
		}
		catch (SystemException e) {
			_Log.error(e);
		}
		catch (IOException e) {
			_Log.error(e);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@SuppressWarnings("deprecation")
	private User updateUser(ActionRequest request, ActionResponse response, long nguoiDungId, String tenDangNhap, String hoVaTen, int gioiTinh) throws Exception {
		User currentUser = UserLocalServiceUtil.fetchUser(nguoiDungId);

		boolean autoPassword = false;
		String password1 = currentUser.getPassword();
		String password2 = currentUser.getPassword();


		String reminderQueryQuestion = "";
		String reminderQueryAnswer = "";

		String screenName = tenDangNhap;
		String emailAddress = tenDangNhap + "@cabd.com";

		/*
		 * some default values for unused fields
		 */
		long facebookId = 0L;
		String openId = StringPool.BLANK;
		String languageId = StringPool.BLANK;
		String timeZoneId = StringPool.BLANK;
		String greeting = StringPool.BLANK;

		/*
		 * name fields
		 */
		String firstName = hoVaTen;
		String middleName = StringPool.BLANK;
		String lastName = StringPool.BLANK;

		int prefixId = 0;
		int suffixId = 0;

		boolean male = gioiTinh == 1 ? true : false;

		/*
		 * birthday information
		 */
		int birthdayMonth = 1;
		int birthdayDay = 1;
		int birthdayYear = 1980;


		/*
		 * social network fields
		 */
		String comments = StringPool.BLANK;
		String smsSn = StringPool.BLANK;
		String aimSn = StringPool.BLANK;
		String facebookSn = StringPool.BLANK;
		String icqSn = StringPool.BLANK;
		String jabberSn = StringPool.BLANK;
		String msnSn = StringPool.BLANK;
		String mySpaceSn = StringPool.BLANK;
		String skypeSn = StringPool.BLANK;
		String twitterSn = StringPool.BLANK;
		String ymSn = StringPool.BLANK;
		String jobTitle = StringPool.BLANK;

		long[] groupIds = null;
		long[] organizationIds = null;
		long[] roleIds = null;
		List<UserGroupRole> userGroupRoles = UsersAdminUtil
				.getUserGroupRoles(request);
		long[] userGroupIds = null;

		ServiceContext serviceContext = ServiceContextFactory.getInstance(
				User.class.getName(), request);

		/*
		 * update user
		 * the id of the Liferay User is same with the id of Nguoi dung
		 */
		User user = UserServiceUtil.updateUser(nguoiDungId,
				StringPool.BLANK, password1, password2, autoPassword,
				reminderQueryQuestion, reminderQueryAnswer, screenName,
				emailAddress, facebookId, openId, languageId, timeZoneId,
				greeting, comments, firstName, middleName, lastName,
				prefixId, suffixId, male, birthdayMonth, birthdayDay,
				birthdayYear, smsSn, aimSn, facebookSn, icqSn, jabberSn,
				msnSn, mySpaceSn, skypeSn, twitterSn, ymSn, jobTitle,
				groupIds, organizationIds, roleIds, userGroupRoles,
				userGroupIds, serviceContext);

		return user;
	}
	public void updateMatKhauNguoiDung(ActionRequest actionRequest, ActionResponse actionResponse) {
		FLog.Log("updateMatKhauNguoiDung");
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		try {
			User user = PortalUtil.getUser(actionRequest);
			if (Validator.isNotNull(user)) {
				
				String matKhau = ParamUtil.getString(actionRequest, "matKhau");
				long nguoiDungId = ParamUtil.getLong(actionRequest, "IDQLKT_NguoiDung");
				updateMatKhauUser(actionRequest,actionResponse, nguoiDungId, matKhau);
				SessionMessages.add(actionRequest, "cap-nhat-nguoi-dung-thanh-cong");
			}else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}

			if (redirect != null && redirect.length() > 0)
				actionResponse.sendRedirect(redirect);

		}
		catch (PortalException e) {
			_Log.error(e);
		}
		catch (SystemException e) {
			_Log.error(e);
		}
		catch (IOException e) {
			_Log.error(e);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("deprecation")
	private User updateMatKhauUser(ActionRequest request, ActionResponse response, long nguoiDungId, String password) throws Exception {
		User currentUser = UserLocalServiceUtil.fetchUser(nguoiDungId);

		boolean autoPassword = false;
		String password1 = password;
		String password2 = password;


		String reminderQueryQuestion = "";
		String reminderQueryAnswer = "";

		String screenName = currentUser.getScreenName();
		String emailAddress = currentUser.getScreenName() + "@cabd.com";

		/*
		 * some default values for unused fields
		 */
		long facebookId = 0L;
		String openId = StringPool.BLANK;
		String languageId = StringPool.BLANK;
		String timeZoneId = StringPool.BLANK;
		String greeting = StringPool.BLANK;

		/*
		 * name fields
		 */
		String firstName = currentUser.getFirstName();
		String middleName = StringPool.BLANK;
		String lastName = StringPool.BLANK;

		int prefixId = 0;
		int suffixId = 0;

		boolean male = currentUser.getMale();

		/*
		 * birthday information
		 */
		int birthdayMonth = 1;
		int birthdayDay = 1;
		int birthdayYear = 1980;


		/*
		 * social network fields
		 */
		String comments = StringPool.BLANK;
		String smsSn = StringPool.BLANK;
		String aimSn = StringPool.BLANK;
		String facebookSn = StringPool.BLANK;
		String icqSn = StringPool.BLANK;
		String jabberSn = StringPool.BLANK;
		String msnSn = StringPool.BLANK;
		String mySpaceSn = StringPool.BLANK;
		String skypeSn = StringPool.BLANK;
		String twitterSn = StringPool.BLANK;
		String ymSn = StringPool.BLANK;
		String jobTitle = StringPool.BLANK;

		long[] groupIds = null;
		long[] organizationIds = null;
		long[] roleIds = null;
		List<UserGroupRole> userGroupRoles = UsersAdminUtil
				.getUserGroupRoles(request);
		long[] userGroupIds = null;

		ServiceContext serviceContext = ServiceContextFactory.getInstance(
				User.class.getName(), request);

		/*
		 * update user
		 * the id of the Liferay User is same with the id of Nguoi dung
		 */
		User user = UserServiceUtil.updateUser(nguoiDungId,
				StringPool.BLANK, password1, password2, autoPassword,
				reminderQueryQuestion, reminderQueryAnswer, screenName,
				emailAddress, facebookId, openId, languageId, timeZoneId,
				greeting, comments, firstName, middleName, lastName,
				prefixId, suffixId, male, birthdayMonth, birthdayDay,
				birthdayYear, smsSn, aimSn, facebookSn, icqSn, jabberSn,
				msnSn, mySpaceSn, skypeSn, twitterSn, ymSn, jobTitle,
				groupIds, organizationIds, roleIds, userGroupRoles,
				userGroupIds, serviceContext);

		return user;
	}
	private void logChangedValues(User user, NguoiDung oldObject, NguoiDung newObject, String thaoTac)
			throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, PortalException, SystemException {
		LichSuThaoTacUtil.addLichSuThaoTac(MaPhanMem.QLKTCSDLDCDC.toString(), LichSuThaoTacEnum.QLKT_NGUOI_DUNG.getName(), thaoTac, (Object)oldObject, (Object)newObject, user);
	}
	
	public void QLTK_ManHinhChinhAction(ActionRequest actionRequest, ActionResponse actionResponse) {
		String cmd = ParamUtil.getString(actionRequest, "cmd");	
		FLog.Log("QLTK_ManHinhChinhAction cmd >>>>>>>>>>>>>>"+cmd);
		if (cmd.equals(QLKTCSDLDCContants.IN)){
			QLTK_ManHinhChinhIn(actionRequest, actionResponse);
		} else if(cmd.equals(QLKTCSDLDCContants.ADD)){
			String redirect = ParamUtil.getString(actionRequest, "addRedirect");
			if (redirect != null && redirect.length() > 0)
				try {
					actionResponse.sendRedirect(redirect);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}

	private void QLTK_ManHinhChinhIn(ActionRequest actionRequest, ActionResponse actionResponse) {
		// TODO Auto-generated method stub
		FLog.Log("QLTK_ManHinhChinhIn");
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
		String redirect = ParamUtil.getString(actionRequest, "redirect");
		PortletConfig portletConfig = (PortletConfig) actionRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);

		try {
			User user = PortalUtil.getUser(actionRequest);
			if (Validator.isNotNull(user)) {
				String tenSoBanNganh = ParamUtil.getString(actionRequest, "tenSoBanNganh");
				String tenDangNhap = ParamUtil.getString(actionRequest, "tenDangNhap");
				String hoTenNguoiDung = ParamUtil.getString(actionRequest, "hoTenNguoiDung");
				List<NguoiDung> results = NguoiDungLocalServiceUtil.searchNguoiDung(tenSoBanNganh, tenDangNhap, hoTenNguoiDung);
				
				List<TaiKhoanReport> taiKhoanReports = new ArrayList<TaiKhoanReport>();
				for(int i=0; i < results.size(); i++){
					NguoiDung nguoiDung = results.get(i);
					TaiKhoanReport taiKhoanReport = new TaiKhoanReport();
					String soBanNganh = "";
					if(nguoiDung.getIDQLKT_DanhMucSoBanNganh() > 0){
						DanhMucSoBanNganh danhMucSoBanNganh = DanhMucSoBanNganhLocalServiceUtil.fetchDanhMucSoBanNganh(nguoiDung.getIDQLKT_DanhMucSoBanNganh());
						if(Validator.isNotNull(danhMucSoBanNganh)){
							soBanNganh = danhMucSoBanNganh.getTenSoBanNganh();
						}
					}
					String trangThai = "";
					if(nguoiDung.getKichHoat() ){
						trangThai = LanguageUtil.get(portletConfig, actionRequest.getLocale(), "da-kich-hoat");
					}else{
						trangThai = LanguageUtil.get(portletConfig, actionRequest.getLocale(), "vo-hieu-hoa");
					}
					String chuThich = nguoiDung.getChuThich();
					
					taiKhoanReport.setStt((i+1)+"");
					taiKhoanReport.setTenDangNhap(nguoiDung.getTenDangNhap());
					taiKhoanReport.setTrangThai(trangThai);
					taiKhoanReport.setHoVaTen(nguoiDung.getHoVaTen());
					taiKhoanReport.setSoBanNganh(soBanNganh);
					taiKhoanReport.setChuThich(chuThich);
					
					taiKhoanReports.add(taiKhoanReport);
				}
			
				// create file PDF
				HashMap<String, Object> data = new HashMap<String, Object>();
				data.put("tenSoBanNganh", tenSoBanNganh);
				data.put("tenDangNhap", tenDangNhap);
				data.put("hoTenNguoiDung", hoTenNguoiDung);
				data.put("list_taikhoan", taiKhoanReports);
				
				PortletContext pc = actionRequest.getPortletSession().getPortletContext();
				String reportFileName = "Quan_ly_tai_khoan";
				String reportFilePath = pc.getRealPath(QLKTCSDLDCContants.REPORT_FOLDER + "/" + reportFileName + ".jrxml");

				File file = Report.createReport(data, Report.DOC_TYPE, reportFilePath, reportFileName);
				if(Validator.isNotNull(file)) {
					InputStream in = new FileInputStream(file);

					HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(actionRequest);
					HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(actionResponse);
					ServletResponseUtil.sendFile(httpServletRequest, httpServletResponse, reportFileName + "." + Report.DOC_TYPE, in, Report.DOC_STREAM_TYPE);
				}
				
				SessionMessages.add(actionRequest, "file-in-duoc-mo-vui-long-kiem-tra-file-in");

				FLog.Log("QLTK_ManHinhChinhIn  cap-nhat-thanh-cong:");
			}
			else {
				SessionErrors.add(actionRequest, "vui-long-dang-nhap");
			}

			if (redirect != null && redirect.length() > 0)
				actionResponse.sendRedirect(redirect);

		}
		catch (PortalException e) {
			_Log.error(e);
		}
		catch (SystemException e) {
			_Log.error(e);
		}
		catch (IOException e) {
			_Log.error(e);
		}
	}
}
